import threading

import jmespath

from config_params.base_util import baseUtil


class Listeners:
    def __init__(self, data):
        self.data = data

    def jme_path(self, exp):
        return jmespath.search(exp, self.data)

    def ViewResultsTree(self, listener_obj):
        view_dict = {}
        view_dict["name"] = self.jme_path("name") if self.jme_path("name") is not None else "View Results Tree"
        view_dict["comments"] = self.jme_path("comments") if self.jme_path("comments") is not None else ""
        view_dict["error_logging"] = self.jme_path("error_logging") if self.jme_path("error_logging") is not None else False
        view_dict["filename"] = self.jme_path("filename") if self.jme_path("filename") is not None else None
        view_dict["time"] = self.jme_path("time") if self.jme_path("time") is not None else True
        view_dict["latency"] = self.jme_path("latency") if self.jme_path("latency") is not None else True
        view_dict["timestamp"] = self.jme_path("timestamp") if self.jme_path("timestamp") is not None else True
        view_dict["success"] = self.jme_path("success") if self.jme_path("success") is not None else True
        view_dict["label"] = self.jme_path("label") if self.jme_path("label") is not None else True
        view_dict["code"] = self.jme_path("code") if self.jme_path("code") is not None else True
        view_dict["message"] = self.jme_path("message") if self.jme_path("message") is not None else True
        view_dict["thread_name"] = self.jme_path("thread_name") if self.jme_path("thread_name") is not None else True
        view_dict["data_type"] = self.jme_path("data_type") if self.jme_path("data_type") is not None else True
        view_dict["encoding"] = self.jme_path("encoding") if self.jme_path("encoding") is not None else False
        view_dict["assertions"] = self.jme_path("assertions") if self.jme_path("assertions") is not None else False
        view_dict["subresults"] = self.jme_path("subresults") if self.jme_path("subresults") is not None else True
        view_dict["response_data"] = self.jme_path("response_data") if self.jme_path("response_data") is not None else False
        view_dict["sampler_data"] = self.jme_path("sampler_data") if self.jme_path("sampler_data") is not None else False
        view_dict["xml"] = self.jme_path("xml") if self.jme_path("xml") is not None else False
        view_dict["field_names"] = self.jme_path("field_names") if self.jme_path("field_names") is not None else False
        view_dict["response_headers"] = self.jme_path("response_headers") if self.jme_path("response_headers") is not None else False
        view_dict["request_headers"] = self.jme_path("request_headers") if self.jme_path("request_headers") is not None else False
        view_dict["response_data_on_error"] = self.jme_path("response_data_on_error") if self.jme_path("response_data_on_error") is not None else False
        view_dict["save_assertion_results_failure_message"] = self.jme_path("save_assertion_results_failure_message") if self.jme_path("save_assertion_results_failure_message") is not None else False
        view_dict["bytes_"] = self.jme_path("bytes_") if self.jme_path("bytes_") is not None else True
        view_dict["sent_bytes"] = self.jme_path("sent_bytes") if self.jme_path("sent_bytes") is not None else True
        view_dict["url"] = self.jme_path("url") if self.jme_path("url") is not None else True
        view_dict["thread_counts"] = self.jme_path("thread_counts") if self.jme_path("thread_counts") is not None else True
        view_dict["idle_time"] = self.jme_path("idle_time") if self.jme_path("idle_time") is not None else True
        view_dict["connect_time"] = self.jme_path("connect_time") if self.jme_path("connect_time") is not None else True
        view_dict["is_enabled"] = self.jme_path("is_enabled") if self.jme_path("is_enabled") is not None else True
        listener_obj = baseUtil.create_instance(listener_obj, view_dict)
        return listener_obj

    def listeners(self):
        listener_obj_list = []
        listener_obj = self.jme_path("type")
        if listener_obj == "ViewResultsTree":
            listener_obj_result = self.ViewResultsTree(listener_obj)
            listener_obj_list.append(listener_obj_result)
        return listener_obj_list